/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.sms.dao.StoreDiagnoseDetailDao;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseDetail;

/**
 * 店面诊断结果详细Service
 * @author liujiang
 * @version 2013-08-01
 */
@Component
@Transactional(readOnly = true)
public class StoreDiagnoseDetailService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreDiagnoseDetailService.class);
	
	@Autowired
	private StoreDiagnoseDetailDao storeDiagnoseDetailDao;
	
	public StoreDiagnoseDetail get(Long id) {
		return storeDiagnoseDetailDao.findOne(id);
	}
	
	public Page<StoreDiagnoseDetail> find(Page<StoreDiagnoseDetail> page,  Long parentId, String searchName) {
		DetachedCriteria dc = storeDiagnoseDetailDao.createDetachedCriteria();
		if (parentId != null){
			dc.add(Restrictions.eq("storeDiagnose.id", parentId));
		}
		if (StringUtils.isNotEmpty(searchName)){
			dc.createAlias("storeDiagnoseTemplateDetail", "a");
			dc.add(Restrictions.like("a.item_group", "%"+searchName+"%"));
		}
		dc.addOrder(Order.asc("storeDiagnoseTemplateDetail.id"));
		return storeDiagnoseDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreDiagnoseDetail storeDiagnoseDetail) {
		storeDiagnoseDetailDao.save(storeDiagnoseDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeDiagnoseDetailDao.delete(id);
	}
}
